﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Drawing;
using LogLib;
using System.Reflection;
using AppDataStruct;

namespace EECMISWS
{
    /// <summary>
    /// WebService 服务接口调用命令
    /// </summary>
    public enum WebSerCMD : uint
    {
        Login = 0,
        Upload = 1,
        Download = 2,
        GetInfo = 3
    }

    public static class Common
    {
        public static bool SaveAsXml(this PreApplyInfo appInfo, string xmlPath = "")
        {
            if (string.IsNullOrEmpty(xmlPath))
            {
                Serial serial = new Serial();
                xmlPath = serial.GetPersonXmlFile(appInfo.SFZH);//xml文件路径
            }
            if (File.Exists(xmlPath))
            {
                File.Delete(xmlPath);
            }
            XmlExplain xmlExplain = new XmlExplain();
            try
            {
                xmlExplain.ClassToXmlFile(appInfo, xmlPath);
                return File.Exists(xmlPath);
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                Log.WriteAsync("将查询信息保存到XML文件时发生异常:" + ex.Message);
                return false;
            }
        }

        public static void ByteToDat(byte[] bData, string strTplFilePatch)
        {
            MemoryStream m = new MemoryStream(bData);

            //将指纹模板文件放在当前目录下，保存为e.wav；当然如果是图片，可以保存为a.jpg
            FileStream fs = new FileStream(strTplFilePatch, FileMode.OpenOrCreate);
            m.WriteTo(fs);
            m.Close();
            fs.Close();
            m = null;
            fs = null;
        }

        public static byte[] DatToByte(string strDatPath)
        {
            FileStream stream = new FileInfo(strDatPath).OpenRead();
            Byte[] buffer = new Byte[stream.Length];
            //从流中读取字节块并将该数据写入给定缓冲区buffer中
            stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
            stream.Close();
            return buffer;     
        }

        /// <summary>
        /// byte[]转换成Base64String
        /// </summary>
        /// <param name="bFile"></param>
        /// <returns></returns>
        public static string ByteToBase64String(byte[] bFile)
        {
            try
            {
                string strBase64String = Convert.ToBase64String(bFile);
                //strBase64String = strBase64String.Replace("+", "%2B");
                return strBase64String;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Base64String转换成Bitmap
        /// </summary>
        /// <param name="strBase64"></param>
        /// <returns></returns>
        public static Bitmap Base64StringToImg(string strBase64)
        {
            try
            {
                byte[] bBase64 = Convert.FromBase64String(strBase64);
                MemoryStream ms = new MemoryStream(bBase64);
                Bitmap bit = new Bitmap(ms);
                //ms.Close();
                return bit;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        /// <summary>  
        /// 生成反射过来的MethodInfo到指定类型的委托  
        /// </summary>  
        /// <typeparam name="T">EventArgs泛型类型</typeparam>  
        /// <param name="instance">当前对象</param>  
        /// <param name="method">需要转化为delegate的方法</param>  
        /// <returns></returns>  
        public static EventHandler<T> CreateDelegateFromMethodInfo<T>(Object instance, MethodInfo method) where T : EventArgs//约束泛型T只能是来自EventArgs类型的  
        {
            if (method == null)
                return null;
            Delegate del = Delegate.CreateDelegate(typeof(EventHandler<T>), instance, method);
            EventHandler<T> mymethod = del as EventHandler<T>;
            return mymethod;
        }

        /// <summary>  
        /// 生成反射过来的MethodInfo到指定类型的委托  
        /// </summary>  
        /// <typeparam name="T">EventHandle泛型类型</typeparam>  
        /// <param name="instance">当前对象</param>  
        /// <param name="method">需要转化为delegate的方法</param>  
        /// <returns></returns>  
        public static EventHandler CreateDelegateFromMethodInfoByDelegate<T>(Object instance, MethodInfo method)
        {
            if (method == null)
                return null;
            Delegate del = Delegate.CreateDelegate(typeof(T), instance, method);
            EventHandler mymethod = del as EventHandler;
            return mymethod;
        }

        public static Size GetSize(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return Size.Empty;
            }
            int begin = value.IndexOf("(");
            int end = value.IndexOf(")");
            if (begin >= 0 && begin >= 0)
            {
                value = value.Substring(begin + 1, end - begin - 1);
            }
            string[] arr = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (arr.Length == 2)
            {
                int w = Convert.ToInt32(arr[0]);
                int h = Convert.ToInt32(arr[1]);
                return new Size(w, h);
            }
            return Size.Empty;
        }

        public static bool ConvertToBoolean(string tStr, bool defaultVal = true)
        {
            if (string.IsNullOrEmpty(tStr))
            {
                return defaultVal;
            }
            bool value = defaultVal;
            bool.TryParse(tStr, out value);
            return value;
        }

        public static int ConvertToInt32(string tStr, int defaultVal = 120)
        {
            if (string.IsNullOrEmpty(tStr))
            {
                return defaultVal;
            }
            int value = defaultVal;
            int.TryParse(tStr, out value);
            return value;
        }

        public static Color GetColor(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return Color.Empty;
            }
            value = value.Trim();
            if (value.Contains("(") && value.Contains(")"))
            {
                int begin = value.IndexOf("(");
                int end = value.IndexOf(")");
                string realVal = value.Substring(begin + 1, end - begin - 1);
                string[] arr = realVal.Split(',');
                if (arr.Length == 3)
                {
                    int r = Convert.ToInt32(arr[0]);
                    int g = Convert.ToInt32(arr[1]);
                    int b = Convert.ToInt32(arr[2]);
                    return Color.FromArgb(r, g, b);
                }
                if (arr.Length == 4)
                {
                    int a = Convert.ToInt32(arr[0]);
                    int r = Convert.ToInt32(arr[1]);
                    int g = Convert.ToInt32(arr[2]);
                    int b = Convert.ToInt32(arr[3]);
                    return Color.FromArgb(a, r, g, b);
                }
            }
            if (!value.Contains(","))
            {
                KnownColor kCol;
                if (Enum.TryParse<KnownColor>(value, out kCol))
                {
                    return Color.FromKnownColor(kCol);
                }

                Type colorType = typeof(Color);
                PropertyInfo info = colorType.GetProperty(value, BindingFlags.Public | BindingFlags.Static);
                if (info != null)
                {
                    return (Color)info.GetValue(null, null);
                }

                Type colorType1 = typeof(System.Drawing.SystemColors);
                PropertyInfo info1 = colorType1.GetProperty(value, BindingFlags.Public | BindingFlags.Static);
                if (info1 != null)
                {
                    return (Color)info1.GetValue(null, null);
                }
            }
            else
            {
                string[] array = value.Split(',');
                if (array.Length == 3)
                {
                    int r = Convert.ToInt32(array[0]);
                    int g = Convert.ToInt32(array[1]);
                    int b = Convert.ToInt32(array[2]);
                    return Color.FromArgb(r, g, b);
                }
                if (array.Length == 4)
                {
                    int a = Convert.ToInt32(array[0]);
                    int r = Convert.ToInt32(array[1]);
                    int g = Convert.ToInt32(array[2]);
                    int b = Convert.ToInt32(array[3]);
                    return Color.FromArgb(a, r, g, b);
                }
            }
            return Color.Empty;
        }

        private static Font GetFont(string[] values)
        {
            string fontName = values[0];
            string fontSize = values[1];
            if (fontSize.EndsWith("F") || fontSize.EndsWith("f"))
            {
                fontSize = fontSize.Substring(0, fontSize.Length - 1);
            }
            float size = 12f;
            float.TryParse(fontSize, out size);
            FontStyle style = FontStyle.Regular;
            if (values.Length == 3)
            {
                string fontStyle = values[2];
                style = (FontStyle)Enum.Parse(typeof(FontStyle), fontStyle);
            }
            return new Font(fontName, size, style);
        }

        public static Font GetFont(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }
            if (value.Contains("(") && value.Contains(")"))
            {
                int begin = value.IndexOf("(");
                int end = value.IndexOf(")");
                string realVal = value.Substring(begin + 1, end - begin - 1);
                string[] arr = realVal.Split(',');
                if (arr.Length == 3 || arr.Length == 2)
                {
                    return GetFont(arr);
                }
            }
            string[] array = value.Split(',');
            if (array.Length == 3 || array.Length == 2)
            {
                return GetFont(array);
            }
            return null;
        }

        private static Point GetLocation(string[] values)
        {
            int x = Convert.ToInt32(values[0]);
            int y = Convert.ToInt32(values[1]);
            return new Point(x, y);
        }

        public static Point GetLocation(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return Point.Empty;
            }
            if (value.Contains("(") && value.Contains(")"))
            {
                int begin = value.IndexOf("(");
                int end = value.IndexOf(")");
                string realVal = value.Substring(begin + 1, end - begin - 1);
                string[] arr = realVal.Split(',');
                if (arr.Length == 2)
                {
                    return GetLocation(arr);
                }
            }
            string[] array = value.Split(',');
            if (array.Length == 2)
            {
                return GetLocation(array);
            }
            return Point.Empty;
        }

    }
}
